#define _CRT_SECURE_NO_WARNINGS
#include<vector>
using namespace std;
class Solution
{
    bool check[51][51];
    int dx[4] = { -1,1,0,0 };
    int dy[4] = { 0,0,-1,1 };
    int m, n;
    int sum = 0;
public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int ret = INT_MIN;
        m = grid.size();
        n = grid[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!check[i][j] && grid[i][j] == 1)
                {
                    sum = 1;
                    check[i][j] = true;
                    ret = max(ret, dfs(grid, i, j));
                }
            }
        }
        return ret == INT_MIN ? 0 : ret;
    }
    int dfs(vector<vector<int>>& grid, int i, int j)
    {

        for (int k = 0; k < 4; k++)
        {
            int x = dx[k] + i;
            int y = dy[k] + j;
            if (x >= 0 && x < m && y >= 0 && y < n && !check[x][y] && grid[x][y] == 1)
            {
                check[x][y] = true;
                sum++;
                dfs(grid, x, y);
            }
        }
        return sum;
    }
};
int main()
{
    vector<vector<int>> arr = { 
        {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
        {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
        {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};
    Solution().maxAreaOfIsland(arr);
    return 0;
}